package com.yunhe.common.interceptor;

import com.yunhe.common.filter.ServerContext;
import com.yunhe.common.filter.ServerContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class ServerContextInterceptor implements RequestInterceptor {

    private static Logger logger = LoggerFactory.getLogger(ServerContextInterceptor.class);

    /**
     * 将PROJECT_CODE放进请求头中   为了Feign调用时不丢失
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        logger.info("[ServerContextInterceptor Feign invoke ] current thread is ：{}. this configuration is come in url is :{}",
                Thread.currentThread().getName(),requestTemplate.url());
        logger.info("[ServerContextInterceptor Feign invoke ] projectCode : {}", ServerContextHolder.getContext().getProjectCode());
        requestTemplate.header(ServerContext.PROJECT_CODE, ServerContextHolder.getContext().getProjectCode());
        requestTemplate.header(ServerContext.ACCESS_TOKEN, ServerContextHolder.getContext().getAccessToken());
    }
}
